package org.jgs1904.work.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.jgs1904.util.DataSourceUtil;
import org.jgs1904.work.entity.Users;

/**
 * 用户数据操作类
 * @author dyf
 * @date 2020年2月4日
 */
public class UserDao {

	public Users selectOne(Users users) throws SQLException {

		QueryRunner qr = new QueryRunner(DataSourceUtil.getDataSource());
		
		String sql = "select * from users where 1 = 1";
		List<Object> params = new ArrayList<>();
		
		if (users.getId() != null) {
			sql += " and id = ?";
			params.add(users.getId());
		}
		if (users.getUsername() != null) {
			sql += " and username = ?";
			params.add(users.getUsername());
		}
		if (users.getPassword() != null) {
			sql += " and password = ?";
			params.add(users.getPassword());
		}
		if (users.getReal_name() != null) {
			sql += " and real_name = ?";
			params.add(users.getReal_name());
		}
		
		Users result = qr.query(sql, new BeanHandler<>(Users.class), params.toArray());
		
		return result;
	}

}
